package com.hb.trade.api.routine.controller.project.evaluation;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.enums.SendSMSEnum;
import com.hb.core.pojo.expert.dto.ExpertDTO;
import com.hb.core.pojo.expert.dto.ExpertExtractParam;
import com.hb.core.pojo.expert.dto.ExpertMajorDTO;
import com.hb.core.pojo.expert.dto.MajorAmount;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.evaluation.dto.ConfirmationInfoDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeBidDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeMembersDTO;
import com.hb.core.pojo.project.proj.entity.BidInviterVO;
import com.hb.core.pojo.project.section.dto.BidSectionTenderDTO;
import com.hb.trade.api.routine.controller.util.ScheduleExcelService;
import com.hb.trade.api.routine.controller.util.SendSMSService;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.expert.ExpertClient;
import com.hb.trade.client.project.EvaluationCommitteeClient;
import com.hb.trade.client.project.SectionClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.MsgUtil;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评标模块Api接口
 *
 * @author Lix1ang
 * @date 2021-12-16 11:19:21
 * @update ljs
 */
@Slf4j
@Version(1.0)
@Api(tags = {"评标相关"})
@RequestMapping("/{version}")
@RestController
public class ApiEvaluationCommitteeController {

    protected EvaluationCommitteeClient evaluationCommitteeClient;
    protected final SendSMSService sendSMSService;
    protected final OrgClient orgClient;
    protected final SectionClient sectionClient;
    @Resource
    protected ExpertClient expertClient;

    public ApiEvaluationCommitteeController(EvaluationCommitteeClient evaluationCommitteeClient,
                                            ExpertClient expertClient,
                                            OrgClient orgClient,
                                            SectionClient sectionClient,
                                            SendSMSService sendSMSService) {
        this.evaluationCommitteeClient = evaluationCommitteeClient;
        this.expertClient = expertClient;
        this.sendSMSService = sendSMSService;
        this.orgClient = orgClient;
        this.sectionClient = sectionClient;
    }

    @ApiOperation(value = "根据项目Id查询未被评委会选择的标段信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "id", value = "评委会Id", dataTypeClass = Long.class)
    })
    @PostMapping("/evaluationCommittee/listNotSelectSection")
    public ResponseDto listNotSelectSection(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.listNotSelectSection(evaluationCommitteeDTO));
    }

    @ApiOperation(value = "评标委员会列表分页", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "evaluationCommitteeName", value = "评委会名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "状态", dataTypeClass = Integer.class)
    })
    @PostMapping("/evaluationCommittee/pageEvaluationCommittee")
    public ResponseDto pageEvaluationCommittee(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.pageEvaluationCommittee(evaluationCommitteeDTO));
    }

    @ApiOperation(value = "保存评标委员会信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "evaluationCommitteeName", value = "评委会名称", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "evaluationWay", value = "评委会类型[1: 资格预审, 2: 其他]", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "evaluationType", value = "1: 资格预审, 2: 其他", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "evaluationExpertNum", value = "评标专家人数", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "tenderNum", value = "招标人代表人数", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "evaluationCommitteeNum", value = "评委会总人数", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "evaluationStartTime", value = "评标开始时间", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "evaluationEndTime", value = "评标结束时间", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "evaluationAddress", value = "评标地点", dataTypeClass = String.class),
            @ApiImplicitParam(name = "note", value = "备注", dataTypeClass = String.class),
            @ApiImplicitParam(name = "smsMessage", value = "短信内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sectionList", value = "标段集合", dataTypeClass = List.class),
            @ApiImplicitParam(name = "expertList", value = "专家集合", dataTypeClass = List.class),
            @ApiImplicitParam(name = "randomExpertList", value = "随机抽取专家集合", dataTypeClass = List.class),
            @ApiImplicitParam(name = "bidInviterList", value = "招标人代表集合", dataTypeClass = List.class),
            @ApiImplicitParam(name = "status", value = "状态", dataTypeClass = Integer.class),
    })
    @PostMapping("/evaluationCommittee/save")
    public ResponseDto saveEvaluationCommittee(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        if(evaluationCommitteeDTO.getExpertList()==null && evaluationCommitteeDTO.getRandomExpertList()==null){
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        evaluationCommitteeDTO.setCreateId(ShiroUtil.getUserId());
        evaluationCommitteeDTO.setCreateName(ShiroUtil.getUser().getFullName());
        evaluationCommitteeDTO.setCreateTime(LocalDateTime.now());
        return ResultUtil.msg(evaluationCommitteeClient.saveEvaluationCommittee(evaluationCommitteeDTO));
    }

    @ApiOperation(value = "评委会发布", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", required = true, dataTypeClass = Long.class)
    })
    @PostMapping("/evaluationCommittee/publishEvaluationCommittee")
    public ResponseDto publishEvaluationCommittee(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        Message publishResult = evaluationCommitteeClient.publishEvaluationCommittee(evaluationCommitteeDTO);

        if (publishResult.isSuccess()) {
            // 发送短信通知
            JSONObject params = JSONUtil.createObj()
                    .putOpt("evaluationCommitteeId", evaluationCommitteeDTO.getId());
            sendSMSService.send(SendSMSEnum.EXPERT_REMINDER.getCode(), params);
        }
        return ResultUtil.msg(publishResult);
    }

    @ApiOperation(value = "根据主键Id或者标段Id查询评委会详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionId", value = "标段ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "evaluationType", value = "评委会类型[1: 资格预审, 2: 其他]", dataTypeClass = Integer.class)
    })
    @PostMapping("/evaluationCommittee/detail")
    public ResponseDto evaluationCommitteeDetail(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.evaluationCommitteeDetail(evaluationCommitteeDTO));
    }

    @ApiOperation(value = "获取招标人代表列表" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "已选Id", dataTypeClass = List.class),
            @ApiImplicitParam(name = "organizeId", value = "企业ID", dataTypeClass = String.class)
    })
    @PostMapping("/evaluationCommittee/getBidInviter")
    public ResponseDto getBidInviter(@RequestBody BidInviterVO bidInviterVO) {
        bidInviterVO.setOrganizeId(ShiroUtil.getOrgId());
        return ResultUtil.msg(evaluationCommitteeClient.getBidInviter(bidInviterVO));
    }

    @ApiOperation(value = "获取评委会成员列表" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评委会ID", dataTypeClass = String.class),
    })
    @PostMapping("/evaluationCommittee/listMembers")
    public ResponseDto listMembers(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.listMembers(evaluationCommitteeDTO));
    }
    @ApiOperation(value = "删除评委会成员" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "成员ID", dataTypeClass = String.class),
    })
    @PostMapping("/evaluationCommittee/removeMember")
    public ResponseDto removeMember(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.removeMember(evaluationCommitteeMembersDTO));
    }

    @ApiOperation(value = "随机抽取专家并脱敏" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "majorAmountList", value = "专家专业类型和数量", dataTypeClass = List.class),
            @ApiImplicitParam(name = "avoidEnterprises", value = "回避企业名称", dataTypeClass = List.class),
            @ApiImplicitParam(name = "avoidExperts", value = "专家ID列表", dataTypeClass = List.class),
    })
    @PostMapping("/evaluationCommittee/extractingExpert")
    public ResponseDto extractingExpert(@RequestBody ExpertExtractParam expertExtractParam) {
        Long orgId = ShiroUtil.getOrgId();
        // 查询招标企业
        Message<OrgDTO> orgDTO = orgClient.getOrgById(orgId);
        String orgName = orgDTO.getData().getOrgName();
        List<String> avoidEnterprises =  expertExtractParam.getAvoidEnterprises();
        avoidEnterprises.add(orgName);
        // 查询投标人企业
        if (expertExtractParam.getProjectId() != null){
            List<String> orgList = sectionClient.getTenderOrgByProjectId(expertExtractParam.getProjectId());
            if(CollUtil.isNotEmpty(orgList)){
                avoidEnterprises.addAll(orgList);
            }
        }
        //设置全部回避企业
        expertExtractParam.setAvoidEnterprises(avoidEnterprises);
        // 设置专家库所在的企业
        expertExtractParam.setOrgId(orgId);
        Message<List<ExpertDTO>> expertExtractList = expertClient.expertExtract(expertExtractParam);
        if(null == expertExtractList.getData()){
            return ResultUtil.msg(expertExtractList);
        }
        Message result = evaluationCommitteeClient.extractingExpert(expertExtractList.getData());
        result.setMessage(expertExtractList.getMessage());
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "获取专业列表" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentCode", value = "专业父类Code", dataTypeClass = String.class),
    })
    @PostMapping("/evaluationCommittee/listMajor")
    public ResponseDto listMajor(@RequestBody ExpertMajorDTO expertMajorDTO) {
        expertMajorDTO.setOrgId(ShiroUtil.getOrgId());
        List<ExpertMajorDTO> expertExtractList = expertClient.majorList(expertMajorDTO);
        Message result = MsgUtil.success(expertExtractList);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "获取专业列表" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentCode", value = "专业父类Code", dataTypeClass = String.class),
    })
    @PostMapping("/evaluationCommittee/getAllMajor")
    public ResponseDto getAllMajor(@RequestBody ExpertMajorDTO expertMajorDTO){
        List<ExpertMajorDTO> allMajorList = expertClient.allMajorList(expertMajorDTO);
        return ResultUtil.msg(MsgUtil.success(allMajorList));
    }

    @ApiOperation(value = "获取专家列表" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchParam", value = "手机号/身份证号/姓名", dataTypeClass = String.class),
    })
    @PostMapping("/evaluationCommittee/getExpert")
    public ResponseDto getExpert(@RequestBody ExpertDTO expertDTO) {
        expertDTO.setOrgId(ShiroUtil.getOrgId());
        PageResult<ExpertDTO> expertSearch = expertClient.expertSearch(expertDTO);
        Message result = MsgUtil.success(expertSearch);
        return ResultUtil.msg(result);
    }



    @ApiOperation(value = "重新指定专家" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评委会ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "expertList", value = "指定专家", dataTypeClass = List.class),
            @ApiImplicitParam(name = "deleteExpertIdList", value = "待删除的专家（重新指定时需要）", dataTypeClass = List.class)
    })
    @PostMapping("/evaluationCommittee/reassignExpert")
    public ResponseDto reassignExpert(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.reassignExpert(evaluationCommitteeDTO));
    }

    @ApiOperation(value = "重新指定组长" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评委会ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "expertId", value = "组长id", dataTypeClass = List.class),
    })
    @PostMapping("/evaluationCommittee/reassignLeaderExpert")
    public ResponseDto reassignLeaderExpert(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return ResultUtil.msg(evaluationCommitteeClient.reassignLeaderExpert(evaluationCommitteeDTO));
    }



    @ApiOperation(value = "重新抽取专家" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评委会ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "majorCode", value = "补充抽取的专家住专业", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deleteExpertIdList", value = "待删除的专家（重新指定时需要）", dataTypeClass = List.class)
    })
    @PostMapping("/evaluationCommittee/reassignExtractExpert")
    public ResponseDto reassignExtractExpert(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        evaluationCommitteeDTO.setCreateId(ShiroUtil.getUserId());
        evaluationCommitteeDTO.setCreateName(ShiroUtil.getUser().getFullName());
        // 获取当前评审项的详情
        Message<EvaluationCommitteeDTO> evaluationCommitteeDTOMessage = evaluationCommitteeClient.evaluationCommitteeDetailByPushSms(evaluationCommitteeDTO);
        if(null == evaluationCommitteeDTOMessage.getData()){
            return ResultUtil.fail("未查询到评委会详情");
        }
        // 标记需要过滤的专家信息
        List<String> avoidExpertList = new ArrayList<>();
        List<EvaluationCommitteeMembersDTO> expertList = evaluationCommitteeDTOMessage.getData().getExpertList();
        if (!expertList.isEmpty()) {
            avoidExpertList = expertList.stream().map(e -> e.getMemberMobile()).collect(Collectors.toList());
        }

        // 重新抽取专家
        ExpertExtractParam expertExtractParam = new ExpertExtractParam();
        expertExtractParam.setOrgId(ShiroUtil.getOrgId());
        // 设置抽取类型
        MajorAmount majorAmount = new MajorAmount();
        majorAmount.setMajorCode(evaluationCommitteeDTO.getMajorCode());
        majorAmount.setCount(1);
        List<MajorAmount> majorAmountList = new ArrayList<>();
        majorAmountList.add(majorAmount);
        expertExtractParam.setMajorAmountList(majorAmountList);
        expertExtractParam.setAvoidExperts(avoidExpertList);
        // 获取抽取结果
        Message<List<ExpertDTO>> expertExtractList = expertClient.expertExtract(expertExtractParam);
        if(null == expertExtractList.getData()){
            return ResultUtil.msg(expertExtractList);
        }

        List<EvaluationCommitteeMembersDTO> evaluationCommitteeMembersDTOList = new ArrayList<>();
        for (ExpertDTO expertDTO:expertExtractList.getData()) {
            EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO = new EvaluationCommitteeMembersDTO();
            evaluationCommitteeMembersDTO.setMemberId(expertDTO.getId());
            evaluationCommitteeMembersDTO.setMemberName(expertDTO.getExpertName());
            evaluationCommitteeMembersDTO.setMajor(expertDTO.getMajorName());
            evaluationCommitteeMembersDTO.setMemberMobile(expertDTO.getPhoneNumber());
            evaluationCommitteeMembersDTO.setMemberIdCard(expertDTO.getIdCard());
            evaluationCommitteeMembersDTO.setMemberCompany(expertDTO.getCompany());
            evaluationCommitteeMembersDTO.setMemberType(3);
            evaluationCommitteeMembersDTO.setExtractType(1);

            evaluationCommitteeMembersDTOList.add(evaluationCommitteeMembersDTO);
        }
        // 抽取完成后保存抽取记录
        evaluationCommitteeDTO.setExpertList(evaluationCommitteeMembersDTOList);
        return ResultUtil.msg(evaluationCommitteeClient.reassignExpert(evaluationCommitteeDTO));
    }


    @ApiOperation(value = "导出评委会专家" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评委会ID", dataTypeClass = String.class)
    })
    @PostMapping("/evaluationCommittee/exportCommitteeExpert")
    public ResponseDto exportCommitteeExpert(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        try {
            Message<EvaluationCommitteeDTO> message = evaluationCommitteeClient.detailByExport(evaluationCommitteeDTO);
            if (message.isSuccess()) {
                EvaluationCommitteeDTO committeeDTO = message.getData();
                Attachment attachment = ScheduleExcelService.expertExcel(committeeDTO);
                return null == attachment ? ResultUtil.msg(MsgUtil.fail("生成评委会专家信息失败")) : ResultUtil.msg(MsgUtil.success(attachment));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultUtil.msg(MsgUtil.fail("生成评委会专家信息失败"));
    }

    //**************************** zhongdl 评标系统相关的接口 ***************************
    /**
     *
     * @Description: 专家登录之后获取信息
     * @Author: zhongdl
     * @Date:2022/2/15 9:58
     * @param evaluationCommitteeMembers:
     * @return: {@link ResponseDto }
     **/
    @ApiOperation(value = "专家登录之后获取信息" , httpMethod = "POST")
    @PostMapping("/evaluationCommittee/getEvaluationInfo")
    public ResponseDto getEvaluationInfo(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembers) {
        Long id = ShiroUtil.getUserId();
        evaluationCommitteeMembers.setId(id);
        Message<EvaluationCommitteeMembersDTO> evaluationMessaege = evaluationCommitteeClient.getEvaluationInfo(evaluationCommitteeMembers);
        return ResultUtil.msg(evaluationMessaege);
    }

    @ApiOperation(value = "确认承诺书", httpMethod = "POST")
    @PostMapping("/evaluationCommittee/confirmation")
    public ResponseDto confirmation(@RequestBody ConfirmationInfoDTO confirmationInfoDTO) {
        Long id = ShiroUtil.getUserId();
        confirmationInfoDTO.setId(id);
        Message<Boolean> save = evaluationCommitteeClient.saveConfirmationInfo(confirmationInfoDTO);
        return ResultUtil.msg(save);
    }

    /**
     *
     * @Description: 专家登录之后分页获取评标信息列表
     * @Author: zhongdl
     * @Date:2022/2/15 9:58
     * @param evaluationCommitteeBid:
     * @return: {@link ResponseDto }
     **/
    @ApiOperation(value = "专家登录之后分页获取评标信息列表" , httpMethod = "POST")

    @PostMapping("/evaluationCommittee/pageCommitteeBidBid")
    public ResponseDto pageCommitteeBidBid(@RequestBody EvaluationCommitteeBidDTO evaluationCommitteeBid) {
        JSONObject jsonObject = ShiroUtil.getExtInfo();
        if (jsonObject == null){
            return ResultUtil.fail("不存在评委会");
        }
        Long evaluationCommitteeId = jsonObject.get("evaluationCommitteeId", Long.class);
        if (evaluationCommitteeId == null){
            return ResultUtil.fail("不存在评委会");
        }
        evaluationCommitteeBid.setEvaluationCommitteeId(evaluationCommitteeId);
        Message<PageResult<EvaluationCommitteeBidDTO>> evaluationMessaege = evaluationCommitteeClient.pageCommitteeBidBid(evaluationCommitteeBid);

        for (EvaluationCommitteeBidDTO evaluationBid : evaluationMessaege.getData().getRecords()) {
            BidSectionTenderDTO sectionTenderDTO = new BidSectionTenderDTO();
            sectionTenderDTO.setSectionId(evaluationBid.getSectionId());
            // 只需要查询解密成功的
            sectionTenderDTO.setDecryptFlag(1);
            sectionTenderDTO.setStatus(1);
            Message<List<BidSectionTenderDTO>> detailMsg = sectionClient.listBidSectionTender(sectionTenderDTO);
            evaluationBid.setTenderFileList(detailMsg.getData());
        }

        return ResultUtil.msg(evaluationMessaege);
    }

    @ApiOperation(value = "评标委员会列表" , httpMethod = "POST")
    @PostMapping("/evaluationCommittee/listEvaluationCommitteeMember")
    @Deprecated
    public ResponseDto listEvaluationCommitteeMember(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO) {
        JSONObject jsonObject = ShiroUtil.getExtInfo();
        if (jsonObject == null){
            return ResultUtil.fail("不存在评委会");
        }
        Long evaluationCommitteeId = jsonObject.get("evaluationCommitteeId", Long.class);
        if (evaluationCommitteeId == null){
            return ResultUtil.fail("不存在评委会");
        }

        evaluationCommitteeMembersDTO.setEvaluationCommitteeId(evaluationCommitteeId);
        evaluationCommitteeMembersDTO.setMemberTypes(CollUtil.list(false,1,3));
        Message<List<EvaluationCommitteeMembersDTO>> evaluationMessaege = evaluationCommitteeClient.listEvaluationCommitteeMember(evaluationCommitteeMembersDTO);
        return ResultUtil.msg(evaluationMessaege);
    }

    @ApiOperation(value = "选举组长（投标）列表" , httpMethod = "POST")
    @PostMapping("/evaluationCommittee/listElectedMember")
    public ResponseDto listElectedMember(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO) {
        JSONObject jsonObject = ShiroUtil.getExtInfo();
        if (jsonObject == null){
            return ResultUtil.fail("不存在评委会");
        }
        Long evaluationCommitteeId = jsonObject.get("evaluationCommitteeId", Long.class);
        if (evaluationCommitteeId == null){
            return ResultUtil.fail("不存在评委会");
        }

        evaluationCommitteeMembersDTO.setEvaluationCommitteeId(evaluationCommitteeId);
        evaluationCommitteeMembersDTO.setMemberTypes(CollUtil.list(false,1,3));
        Message<List<EvaluationCommitteeMembersDTO>> evaluationMessaege = evaluationCommitteeClient.listEvaluationCommitteeMember(evaluationCommitteeMembersDTO);

        // 排除自己
        // 20220428需求变更：专家需要也能给自己投票，所以自己的名字也要显示在列表里
//        List<EvaluationCommitteeMembersDTO> data = evaluationMessaege.getData();
//        Long id = ShiroUtil.getUserId();
//        for (EvaluationCommitteeMembersDTO member : data) {
//            if (member.getId().equals(id)) {
//                data.remove(member);
//                break;
//            }
//        }

        return ResultUtil.msg(evaluationMessaege);
    }

    @ApiOperation(value = "评委会成员列表（评标系统中使用）" , httpMethod = "POST")
    @PostMapping("/evaluationCommittee/listCommitteeMember")
    public ResponseDto listCommitteeMember(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO) {
        JSONObject jsonObject = ShiroUtil.getExtInfo();
        if (jsonObject == null){
            return ResultUtil.fail("不存在评委会");
        }
        Long evaluationCommitteeId = jsonObject.get("evaluationCommitteeId", Long.class);
        if (evaluationCommitteeId == null){
            return ResultUtil.fail("不存在评委会");
        }

        evaluationCommitteeMembersDTO.setEvaluationCommitteeId(evaluationCommitteeId);
        evaluationCommitteeMembersDTO.setMemberTypes(CollUtil.list(false,1,2,3));
        Message<List<EvaluationCommitteeMembersDTO>> evaluationMessaege = evaluationCommitteeClient.listEvaluationCommitteeMember(evaluationCommitteeMembersDTO);
        return ResultUtil.msg(evaluationMessaege);
    }

    @ApiOperation(value = "投票组长" , httpMethod = "POST")
    @PostMapping("/evaluationCommittee/vote")
    @ApiImplicitParams({
      @ApiImplicitParam(name = "leaderId", value = "投票的主键id", dataTypeClass = Long.class),

    })
    public ResponseDto vote(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembers) {
        JSONObject jsonObject = ShiroUtil.getExtInfo();

        Long leaderId = evaluationCommitteeMembers.getLeaderId();
        if (leaderId == null){
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }
        Long id = ShiroUtil.getUserId();
        // 20220428需求变更：专家需要能给自己投票
//        if (leaderId.equals(id)){
//            return ResultUtil.fail("不能给自己投票");
//        }
        if (jsonObject == null){
            return ResultUtil.fail("不存在评委会");
        }
        Long evaluationCommitteeId = jsonObject.get("evaluationCommitteeId", Long.class);
        if (evaluationCommitteeId == null){
            return ResultUtil.fail("不存在评委会");
        }

        //查询自己的评委会信息
        evaluationCommitteeMembers.setId(id);
        Message<EvaluationCommitteeMembersDTO> evaluationInfo = evaluationCommitteeClient.getEvaluationInfo(evaluationCommitteeMembers);
        if (evaluationInfo.isFail() || evaluationInfo.getData() == null){
            return ResultUtil.FAIL;
        }
        if (evaluationInfo.getData().getLeaderId() != null){
            return ResultUtil.fail("不能重复投票");

        }
        //更新评委会信息进行投票
        Message<Boolean> booleanMessage = evaluationCommitteeClient.updateCommitteeMembersInfo(evaluationCommitteeMembers);
        if (booleanMessage.isFail() || booleanMessage.getData() == null || !booleanMessage.getData()){
            return ResultUtil.FAIL;
        }
        //判断一下是否投票结束 如果投标结束更新一下组长
          //设置评委会id
        evaluationCommitteeMembers.setEvaluationCommitteeId(evaluationCommitteeId);
        Message<EvaluationCommitteeMembersDTO> voteEndFlag = evaluationCommitteeClient.getVoteEndFlag(evaluationCommitteeMembers);
        return ResultUtil.msg(voteEndFlag);
    }

    @ApiOperation(value = "结束评标" , httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataTypeClass = Long.class),

    })
    @PostMapping("/evaluationCommittee/endEvaluation")
    public ResponseDto endEvaluation(@RequestBody EvaluationCommitteeBidDTO evaluationCommitteeBid) {
        JSONObject jsonObject = ShiroUtil.getExtInfo();
        if (jsonObject == null){
            return ResultUtil.fail("不存在评委会");
        }
        Long evaluationCommitteeId = jsonObject.get("evaluationCommitteeId", Long.class);
        if (evaluationCommitteeId == null){
            return ResultUtil.fail("不存在评委会");
        }
        if (evaluationCommitteeBid.getSectionId() == null){
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }

        evaluationCommitteeBid.setEvaluationCommitteeId(evaluationCommitteeId);
        Message<Boolean> endEvaluation = evaluationCommitteeClient.endEvaluation(evaluationCommitteeBid);
        return ResultUtil.msg(endEvaluation);
    }

}
